#include "connect_server_impl.h"
namespace zdh
{
	void XNAT_TCPServer_Impl::Start()
	{
		m_TaskAttemper.Start(m_StartTaskThreadCount);
		XTaskPrintDateTime * aTimer = new XTaskPrintDateTime(&m_TaskAttemper);
		aTimer->ResetTimer(GetNowDateTime());
		aTimer->setSessionList( getSessionList() );
		m_TaskAttemper.AppendTask(m_TCPServerTask);
	}

	void XNAT_TCPServer_Impl::Wait()
	{
		m_TaskAttemper.Wait();
	}

	void XNAT_TCPServer_Impl::Stop()
	{
		m_TaskAttemper.Stop();
	}

	XNAT_TCPServer_Impl::XNAT_TCPServer_Impl()
		:m_StartTaskThreadCount(10),m_TCPServerTask(NULL)
	{

	}

	void XNAT_TCPServer_Impl::Init()
	{
		m_StartTaskThreadCount = 20;
		m_TCPServerTask = new XTaskTCPServer();
		m_TCPServerTask->setSessionList( getSessionList() );
		m_TCPServerTask->Init();
	}

	bool XTCPEvent::OnListen( const XString & aServerIP,XWord aPort )
	{
		Log->Info("OnListen:%s:%d",aServerIP.c_str(), aPort);
		return true;
	}

	int XTCPEvent::OnSelect()
	{
		Log->Info("OnSelect:2000");
		return 2000;
	}

	bool XTCPEvent::OnSelectError( int aErrorNo )
	{
		Log->Error("OnSelectError(%d)",aErrorNo);
		return true;
	}

	bool XTCPEvent::OnSelectTimeout()
	{
		Log->Error("OnSelectTimeout");
		return true;
	}

	bool XTCPEvent::OnAccept( const XString & aClientIP,XWord aPort )
	{
		Log->Info("OnAccount:%s:%d",aClientIP.c_str(),aPort);
		return true;
	}

	bool XTCPEvent::DoAccept( XSocket aClientSocket,XString aClientIP,XWord aPort )
	{
		Log->Info("DoAccept:%s:%d",aClientIP.c_str(),aPort);
		XClientInfo Info;
		Info.setPort( aPort );
		Info.setIP( aClientIP );
		Info.setClientSocket( &aClientSocket );
		m_SessionList->Lock();
		m_SessionList->getSessionList().Append(Info);
		m_SessionList->Unlock();
		return false;
	}
}
